package com.mazaiting.easy.base.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mazaiting.easy.R;
import com.mazaiting.easy.base.interfaces.IRefresh;
import com.mazaiting.easy.base.presenter.BasePresenter;

/**
 * 带刷新的Activity
 * @author mazaiting
 * @date 2018/3/23
 */

public abstract class BaseRefreshToolbarActivity<T extends BasePresenter> extends BaseToolbarActivity<T> implements IRefresh {
    /**列表*/
    private RecyclerView mRecyclerView;
    /**下拉刷新*/
    private SwipeRefreshLayout mSwipeLayout;
    /**适配器*/
    private BaseQuickAdapter mAdapter;

    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        super.bindView(view, savedInstanceState);
        // 获取 RecyclerView
        mRecyclerView = view.findViewById(R.id.recycler_view);
        // 检测是否为空
        if (null == mRecyclerView) {
            throw new NullPointerException("Please use RecyclerView in your layout file.");
        }
        // 获取 SwipeRefreshLayout
        mSwipeLayout = view.findViewById(R.id.swipe_layout);
        // 检测是否为空
        if (null == mSwipeLayout) {
            throw new NullPointerException("Please use SwipeRefreshLayout in your layout file.");
        }
        // 设置下拉刷新的颜色
        mSwipeLayout.setColorSchemeColors(getColorSchemeColors());
        // 设置布局方向
        mRecyclerView.setLayoutManager(getLayoutManager());
        // 创建适配器
        mAdapter = setAdapter();
        // 打开加载动画
        mAdapter.openLoadAnimation(getLoadAnimation());
        // 设置唯一标识
        mAdapter.setHasStableIds(true);
        // 设置适配器
        mRecyclerView.setAdapter(mAdapter);
    }

    @NonNull
    @Override
    public BaseQuickAdapter getAdapter() {
        return mAdapter;
    }

    @NonNull
    @Override
    public RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    @NonNull
    @Override
    public SwipeRefreshLayout getSwipeLayout() {
        return mSwipeLayout;
    }

    /**
     * 获取加载动画
     * @return 加载动画
     */
    @Override
    public int getLoadAnimation() {
        return BaseQuickAdapter.ALPHAIN;
    }

    /**
     * 获取 RecyclerView 方向
     * @return 布局方向
     */
    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(this);
    }

    /**
     * 获取下拉刷新颜色
     * @return 颜色
     */
    @Override
    public int getColorSchemeColors() {
        return Color.rgb(47,223,189);
    }

    /**
     * 设置列表适配器
     * @return 返回适配器
     */
    @Override
    public abstract BaseQuickAdapter setAdapter();

}
